<?php
/** FlashGameWizard
 * @package schs-cms
 * @subpackage Wizard
 * @author Igor.Barkowsky@gmail.com
 * @copyright 2009
 */

class FlashGameWizard extends Wizard
{
	var $table = 'flash_games';
	var $pfx = 'flash_';
	var $pid = 'obj_id';
	var $id='id';
	var $cat;
	var $com;
	var $rat;
	var $flash_main_cat;
	function __construct(MyDB $db,$tablename = '',$pfx = '',$pid = '')
	{
		$this->db = $db;
		if(strlen($tablename) > 0){$this->table = mysql_real_escape_string($tablename);}
		if(strlen($pfx) > 0){$this->pfx = mysql_real_escape_string($pfx);}
		if(strlen($pid) > 0){$this->pid = $pid;}
		$this->cat = new CategoryWizard($this->db);
		$this->flash_main_cat = $this->cat->db->fetch_one("SELECT * FROM ".$this->cat->table." WHERE ".$this->cat->pname." = '0' AND alias = 'games'");
		$this->com = new CommentWizard($this->db);
	}
	function getFullInfoById($id)
	{
		$item = $this->getItem($id);
		if($item != false)
		{
			$item_id = intval($item['id']);
			// получаем инфу о категории игры
			$item['category'] = $this->cat->getItem($item['category_id']);
			// картинка к игре
			if(intval($item['image']) == 1)
			{
				$item['picture']['small'] = UPLOAD.'flash/'.$item_id.'_small.jpg';
				$item['picture']['med'] = UPLOAD.'flash/'.$item_id.'_med.jpg';
				$item['picture']['full'] = UPLOAD.'flash/'.$item_id.'.jpg';
			}
			else $item['picture'] = false;
			// собственно файл игры
			if(intval($item['file']) == 1)
			{
				$item['swf']['path'] = UPLOAD.'flash/'.$item_id.'.swf';
				$fileinfo = @getimagesize(ROOT_DIR.$item['swf']['path']);
				if($fileinfo != false)
				{
					if(intval($fileinfo[0]) > 570)
					{
						$qoeff = 570 / intval($fileinfo[0]);
						$new_height = intval(intval($fileinfo[1]) * $qoeff);
						$item['swf']['width'] = 570;
						$item['swf']['height'] = $new_height;
					}
					else
					{
						$item['swf']['width'] = $fileinfo[0];
						$item['swf']['height'] = $fileinfo[1];
					}
				}
				else $item['swf'] = false;
			}
			else $item['swf'] = false;
			return $item;
		}
		else return false;
	}
	function getFullInfoByList($list = array(),$orderby = "")
	{
		$items = $this->getItemsByList($list,$orderby);
		if($items != false)
		{
			foreach($items as $k => $item)
			{
				$item_id = intval($item['id']);
				// получаем инфу о категории игры
				$items[$k]['category'] = $this->cat->getItem($item['category_id']);
				// картинка к игре
				if(intval($item['image']) == 1)
				{
					$items[$k]['picture']['small'] = UPLOAD.'flash/'.$item_id.'_small.jpg';
					$items[$k]['picture']['med'] = UPLOAD.'flash/'.$item_id.'_med.jpg';
					$items[$k]['picture']['full'] = UPLOAD.'flash/'.$item_id.'.jpg';
				}
				else $items[$k]['picture'] = false;
				// собственно файл игры
				if(intval($item['file']) == 1)
				{
					$items[$k]['swf']['path'] = UPLOAD.'flash/'.$item_id.'.swf';
					$fileinfo = @getimagesize(ROOT_DIR.$item['swf']['path']);
					if($fileinfo != false)
					{
						if(intval($fileinfo[0]) > 570)
						{
							$qoeff = 570 / intval($fileinfo[0]);
							$new_height = intval(intval($fileinfo[1]) * $qoeff);
							$item['swf']['width'] = 570;
							$item['swf']['height'] = $new_height;
						}
						else
						{
							$item['swf']['width'] = $fileinfo[0];
							$item['swf']['height'] = $fileinfo[1];
						}
					}
					else $items[$k]['swf'] = false;
				}
				else $items[$k]['swf'] = false;
			}
			return $items;
		}
		else return false;
	}
	function getFullInfobyAlias($alias = '')
	{
		if(strlen($alias) > 0)
		{
			$item = $this->db->fetch_one("SELECT * FROM $this->table WHERE $this->id = '".mysql_real_escape_string($alias)."'");
			if($item != false)
			{
				$item_id = intval($item['id']);
				// получаем инфу о категории игры
				$item['category'] = $this->cat->getItem($item['category_id']);
				// картинка к игре
				if(intval($item['image']) == 1)
				{
					$item['picture']['small'] = UPLOAD.'flash/'.$item_id.'_small.jpg';
					$item['picture']['med'] = UPLOAD.'flash/'.$item_id.'_med.jpg';
					$item['picture']['full'] = UPLOAD.'flash/'.$item_id.'.jpg';
				}
				else $item['picture'] = false;
				// собственно файл игры
				if(intval($item['file']) == 1)
				{
					$item['swf']['path'] = UPLOAD.'flash/'.$item_id.'.swf';
					$fileinfo = @getimagesize(ROOT_DIR.$item['swf']['path']);
					if($fileinfo != false)
					{
						if(intval($fileinfo[0]) > 570)
						{
							$qoeff = 570 / intval($fileinfo[0]);
							$new_height = intval(intval($fileinfo[1]) * $qoeff);
							$item['swf']['width'] = 570;
							$item['swf']['height'] = $new_height;
						}
						else
						{
							$item['swf']['width'] = $fileinfo[0];
							$item['swf']['height'] = $fileinfo[1];
						}
					}
					else $item['swf'] = false;
				}
				else $item['swf'] = false;
				return $item;
			}
			else return false;
		}
		else return false;
	}
	/**
	 * FlashGameWizard::makeAlias()
	 * 
	 * @param string $title
	 * @return string || false on error
	 */
	function makeAlias($title = '')
	{
	    $title = strtolower(trim($title));
	
	    $alias = $title;
	    if (strlen($title) > 0)
	    {
	
	        $eng = Array ('a', 'b', 'v', 'g', 'd', 'e', 'e', 'j', 'z', 'i', 'y', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'h', 'c', 'ch', 'sh', 'sch', 'i', '', '', 'e', 'u', 'ya','a', 'b', 'v', 'g', 'd', 'e', 'e', 'j', 'z', 'i', 'y', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'h', 'c', 'ch', 'sh', 'sch', 'i', '', '', 'e', 'u', 'ya');
	        $rus = Array ('а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ы', 'ъ', 'ь', 'э', 'ю', 'я','А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ы', 'Ъ', 'Ь', 'Э', 'Ю', 'Я');
	
	
	        foreach ($rus as $k=>$v)
	        {
	            $alias = str_replace($v, $eng[$k], $alias);
	        }
	
	        $alias = str_replace(" ", "_", $alias);
	        $alias = preg_replace("/\W+/","", $alias);
	        $alias = str_replace("___", "_", $alias);
	        $alias = substr(str_replace("__", "_", $alias),0, 95);
	        /** START 	check if is current alias exsists (by module type) **/
	        $is_exists = $this->db->fetch_one("SELECT count(*) FROM $this->table WHERE alias = '".mysql_real_escape_string($alias)."'");
	        if ($is_exists[0] > 0)
	        {
	            $i=1;$fin = false;
	            while ($fin === false)
	            {
	                $check_alias = $alias."_".$i;
	                $is_exists = $this->db->fetch_one("SELECT count(*) FROM $this->table WHERE alias = '".mysql_real_escape_string($check_alias)."'");
	                if ($is_exists[0] > 0)
	                {
	                    $i++;
	                }
	                else
	                {
	                    $alias = $check_alias;
	                    $fin = true;
	                }
	            }
	        }
	        return $alias;
	        /** END 	check if is current alias exsists (by module type) **/
	    }
	    else
	    {
	        return false;
	    }
	}
	function getComments($obj_id = 0)
	{
		if(intval($obj_id) != 0)
		{
			$comments = $this->com->getCommentsTree('flash',$obj_id);
			if($comments != false and !empty($comments))
			{
				$comments['obj_id'] = intval($obj_id);
				$comments['module'] = 'flash';
				$comments['source'] = $_SERVER['REQUEST_URI'];
				return $comments;
			}
			else return false;
		}
		else return false;
	}
	function getAdditionalInfoByItem($obj = array())
	{
		if(is_array($obj) and !empty($obj) and isset($obj[$this->id]) and intval($obj[$this->id]) != 0)
		{
			$item_id = intval($obj[$this->id]);
			// получаем инфу о категории игры
			if(isset($obj[$this->cat->pid]))
				$item['category'] = $this->cat->getItem($obj[$this->cat->pid]);
			// картинка к игре
			if(isset($obj['image']) and intval($obj['image']) == 1)
			{
				$item['picture']['small'] = UPLOAD.'flash/'.$item_id.'_small.jpg';
				$item['picture']['med'] = UPLOAD.'flash/'.$item_id.'_med.jpg';
				$item['picture']['full'] = UPLOAD.'flash/'.$item_id.'.jpg';
			}
			else $item['picture'] = false;
			// собственно файл игры
			if(isset($obj['file']) and intval($obj['file']) == 1)
			{
				$item['swf']['path'] = UPLOAD.'flash/'.$item_id.'.swf';
				$fileinfo = @getimagesize(ROOT_DIR.$item['swf']['path']);
				if($fileinfo != false)
				{
					if(intval($fileinfo[0]) > 570)
					{
						$qoeff = 570 / intval($fileinfo[0]);
						$new_height = intval(intval($fileinfo[1]) * $qoeff);
						$item['swf']['width'] = 570;
						$item['swf']['height'] = $new_height;
					}
					else
					{
						$item['swf']['width'] = $fileinfo[0];
						$item['swf']['height'] = $fileinfo[1];
					}
				}
				else $item['swf'] = false;
			}
			else $item['swf'] = false;
			return $item;
		}
		else return false;
	}
}
?>